package core.java.sortcode;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

public class myComparatorDemo{
    public static void main(String[] args) {
        List<Person1> list = new ArrayList<Person1>();
        list.add(new Person1(2));
        list.add(new Person1(1));
        list.add(new Person1(3));
        Comparator<Person1> myComparator = new MyComparator();
        Collections.sort(list,myComparator);
        for(Person1 p : list){
            System.out.println(p.getAge());
        }
    }
}
class Person1{
    private int age;
    public Person1(int age){
        this.age = age;
    }
    public int getAge() {
        return age;
    }
    public void setAge(int age) {
        this.age = age;
    }   
}

class MyComparator implements Comparator<Person1>{
    @Override
    public int compare(Person1 p1,Person1 p2){
        return p1.getAge() - p2.getAge();
    }
}